package dd.f;
import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.regex.Matcher;

import java.util.regex.Pattern;



import javax.xml.parsers.FactoryConfigurationError;



public class MainApp {



	public static void main(String[] args) {

		/*

		 * fileInputs = new FileInputStream(file); InputStreamReader

		 * inputStreamReader = new InputStreamReader( fileInputs); fileInputs =

		 * new FileInputStream(file); InputStreamReader inputStreamReader = new

		 * InputStreamReader( fileInputs);

		 */



		// D:\log\log.log

		String path = "d:\\temp\\log.log";

		File file = new File(path);

		if (file.exists() && file.isFile()) {

			// main



			FileInputStream fileInputs = null;

			try {

				fileInputs = new FileInputStream(file);

				InputStreamReader inputStreamReader = new InputStreamReader(

						fileInputs);

				BufferedReader bufferedReader = new BufferedReader(

						inputStreamReader);

				String line = null;

				

				File ipFile = new File("D:\\temp\\z.txt"); 

				//D:\log\ips.txt

				if(!ipFile.exists()){

					ipFile.createNewFile();

				}

				

				FileOutputStream fileOutputStream = new FileOutputStream(ipFile);

				BufferedOutputStream  bufferedOutputStream  = new BufferedOutputStream  (fileOutputStream);

				while ((line = bufferedReader.readLine()) != null) {

					//System.out.println(MainApp.getIP(line));

					

					 

					//bufferedOutputStream.

					fileOutputStream.write(MainApp.getIP(line).getBytes());

					fileOutputStream.write("\n".getBytes());

					 

				}

				fileOutputStream.flush();

				fileOutputStream.close();

			} catch (FileNotFoundException e) {

				// TODO Auto-generated catch block

				e.printStackTrace();

			} catch (IOException e) {

				// TODO Auto-generated catch block

				e.printStackTrace();

			}



		} else {

			System.out.println("not a file or not exits!!");

		}

		

		System.out.println("end");

	}



	/**

	 * 从一串文字中提取一个IP

	 * @param content 待处理的一串文字

	 * @return 

	 */

	public static String getIP(String content){

	    String regUrl = "((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))";

        Pattern p = Pattern.compile(regUrl, Pattern.CASE_INSENSITIVE);

        Matcher m = p.matcher(content);

        boolean blnp = m.find();

        if(blnp==true){// get it

        	return m.group();

        }

		return null;

	}

	

}



